package com.example.tatil.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtils {
    private static String signkey = "itheima";
    private static Long expire = 43200000L;

    public static String generateJwt(Map<String,Object> claims){
//    Map<String, Object> claims = new HashMap<>();
//        claims.put("id",1);
//        claims.put("name","tom");

        String Jwt = Jwts.builder()
            .addClaims(claims)
            .signWith(SignatureAlgorithm.HS256,signkey)
//            .setClaims(claims)//自定义的内容(载荷)
            .setExpiration(new Date(System.currentTimeMillis() +expire))//设置有效期
            .compact();

        return Jwt;

}

    //解析Jwt

    public static Claims pareseJwt(String Jwt){
        Claims claims = Jwts.parser()
                .setSigningKey(signkey)
                .parseClaimsJws(Jwt)
                .getBody();
        return claims;
    }
}
